<script setup>
import {defineAsyncComponent, ref} from "vue";
import {router} from "@inertiajs/vue3";
import {route} from "ziggy-js";
import useTable from "@/composables/useTable";
import useRequest from "@/composables/useRequest.js";
import {Message} from "@arco-design/web-vue";
import AppPage from "@/components/admin/layouts/AppPage.vue";

const FormPopup = defineAsyncComponent(() => import('./components/FormPopup.vue'));


const title = '角色管理 - 权限 - 设置'
const breadcrumbs = ref(['设置', '权限', '角色管理'])

defineProps({
    data: {
        type: Array,
        default: () => []
    },
    pagination: {
        type: Object,
        default: () => ({
            current: 1,
            pageSize: 10,
            total: 0,
            showTotal: true
        })
    },
    options: {
        type: Array,
        default: () => []
    }
})

const {TableRef, bordered, scrollbar, scroll} = useTable()
const {deleteApi} = useRequest('admin.setting.permissions.roles')

const handlePageChange = (page) => {
    router.get(route('admin.setting.permissions.roles.index'), {page: page}, {
            replace: true,
            preserveState: true // 保留其他状态
        }
    )
}

const formPopupRef = ref()
const handleAdd = () => formPopupRef.value?.add()
const handleEdit = (id) => formPopupRef.value?.edit(id)

const handleDelete = async (id) => {
    await deleteApi(id)
    router.reload()
    Message.success('删除成功')
}
</script>

<template>
    <app-page :breadcrumbs :title>
        <a-card :style="{ borderRadius: '8px'}" class="custom-card" title="角色管理">
            <a-space :size="16" class="w-full" direction="vertical" fill>
                <a-space align="start" wrap>
                    <a-button type="primary" @click="handleAdd"> 新增角色</a-button>
                </a-space>

                <a-table
                    ref="TableRef"
                    :bordered
                    :data="data"
                    :pagination
                    :scroll
                    :scrollbar
                    :table-layout-fixed="true"
                    row-key="id"
                    @page-change="handlePageChange"
                >
                    <template #columns>
                        <a-table-column :width="80" data-index="id" title="ID"/>
                        <a-table-column :min-width="100" data-index="name" title="名称"/>
                        <a-table-column :min-width="100" data-index="slug" title="标识"/>
                        <a-table-column :width="200" data-index="description" ellipsis title="描述" tooltip />
                        <a-table-column :width="100" data-index="status" title="状态">
                            <template #cell="{ record }">
                                <a-tag :color="record.status === 1 ? 'cyan' : 'red'">
                                    {{ record.status === 1 ? '正常' : '禁用' }}
                                </a-tag>
                            </template>
                        </a-table-column>
                        <a-table-column :width="100" data-index="sort" title="排序"/>
                        <a-table-column :width="120" data-index="created_at" title="创建时间"/>
                        <a-table-column :width="120" fixed="right" title="操作">
                            <template #cell="{ record }">
                                <a-space>
                                    <a-link @click="handleEdit(record.id)"> 编辑</a-link>

                                    <a-popconfirm
                                        content="您确定要删除该项吗?"
                                        type="warning"
                                        @ok="() => handleDelete(record.id)"
                                    >
                                        <a-link status="danger"> 删除</a-link>
                                    </a-popconfirm>
                                </a-space>
                            </template>
                        </a-table-column>
                    </template>
                </a-table>

            </a-space>
            <form-popup ref="formPopupRef"  @success="router.reload()"/>
        </a-card>
    </app-page>
</template>

<style scoped>

</style>
